package com.trsj.lnsj.util;

import java.util.Random;

public class AssignQuestionUtil {

  /**
   * 将你的题的总数循环生成一个数组，比如有10000道题那就生成长度为10000的数组 在这个数组中随机产生一个数，然后把他放到待选数组的最后，
   * 然后从length-1里随机产生下一个随机数，如此类推
   * 
   * @param:sumNum 要生成的数的范围
   * @param:num 生成的个数
   * @return int[] 返回生成的不重复的随机数组
   */
  public static int[] randoms(int sumNum, int num) {
    int returnValue[] = null;
    if (num <= sumNum) {
      returnValue = new int[num];
      Random r = new Random();

      int temp1, temp2;
      // 生成范围数组
      int send[] = new int[sumNum];
      int len = send.length;
      for (int i = 0; i < len; i++) {
        send[i] = i + 1;
      }

      for (int i = 0; i < num; i++) {
        temp1 = Math.abs(r.nextInt()) % len;
        returnValue[i] = send[temp1];
        temp2 = send[temp1];
        send[temp1] = send[len - 1];
        send[len - 1] = temp2;
        len--;
      }
    }
    return returnValue;
  }

  public static void main(String... args) {
    int[] sdf = randoms(1000, 5);
    System.out.println(sdf[0] + "," + sdf[1] + "," + sdf[2] + "," + sdf[3] + "," + sdf[4]);
  }
}
